--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 机器人逻辑

local skynet = require "skynet"
local log = require "Log"
local Network = require "Network"
--local roomConfig = require "lua_ZfbTableList"
local NodeMessage = require "NodeMessage"
require "setting"
local Logic = class("Logic")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function Logic:ctor(data)
	--随机种子
    local time = tostring(os.time())
    math.randomseed(time:reverse():sub(1, 7))
	local svr_config = robot_setting['rt_mingpaipinshi']
	self.playerid = data.playerid
	self.tableid = data.tableid
	self.player = nil
	-- self.user = user
	self.ip = svr_config.ip
	self.port = svr_config.port		
	--[[self.tbConf = {} --场次配置
	self.robotType = data.type
	for _,v in pairs(roomConfig) do 
		table.insert(self.tbConf,v)
	end
	table.sort(self.tbConf,function (a,b)
		if a.min_coin < b.min_coin then 
			return true
		end
	end)]]--
	--self.roomConf = self.tbConf[self.robotType] --要入的场
	self.onlineTime = 0
	self.ExitTime = math.random(10,30) * 60
	self.playingTime = 0
	self.leaveTableTime = math.random(3,9) * 60
	self.heartTickTime = 500

	self.game = data.game or 0	

	self.gameMgrName = "rt_mingpaipinshi"
	self.isSeatdown =false
	self.nodeMessage = NodeMessage.new()
	--开启网络模块
	self.network = Network.new(self.ip,self.port)
	--注册消息回调
	self:register()

	--self:login(self.playerid)
	self:loginChildGame()
	self:closeByTime()
end

function Logic:register()
	self.network:register('php_res',handler(self,self.php_res))
	self.network:register('logout_res',handler(self,self.logout_res))
	self.network:register('room_state_res',handler(self,self.room_state_res))
	self.network:register('enterprivate_room_res',handler(self,self.enter_table_res))
	self.network:register('leave_table_res',handler(self,self.leave_table_res))	
	self.network:register('changetable_res',handler(self,self.changetable_res))		
	self.network:register('nt_broadcast',handler(self,self.nt_broadcast))
	self.network:register('nt_goods_change',handler(self,self.nt_goods_change))
	self.network:register('nt_enter_gate',handler(self,self.nt_enter_gate))
	self.network:register('nt_kick_table',handler(self,self.nt_kick_table))
	self.network:register('heart_res',handler(self,self.heart_res))
	
	self.network:register('zfb_player_res',handler(self,self.zfb_player_res))
	
	self.network:register('exit',handler(self,self.exit))

	self.network:register('rt_mpps_nt_rest',handler(self,self.ps_nt_rest))
	self.network:register('rt_mpps_nt_start',handler(self,self.ps_nt_start))
	self.network:register('rt_mpps_nt_bank',handler(self,self.ps_nt_bank))
	self.network:register('rt_mpps_nt_end',handler(self,self.ps_nt_end))
	self.network:register('rt_mpps_nt_bet',handler(self,self.ps_nt_bet))
	self.network:register('rt_mpps_nt_left_card',handler(self,self.ps_nt_left_card))
	self.network:register('rt_mpps_nt_seat_change',handler(self,self.ps_nt_seat_change))
	self.network:register('rt_mpps_nt_bank_ok',handler(self,self.ps_nt_bank_ok))
	self.network:register('rt_mpps_nt_random_bank_ok',handler(self,self.ps_nt_random_bank_ok))
	self.network:register('rt_mpps_nt_sb_bank',handler(self,self.ps_nt_sb_bank))
	self.network:register('rt_mpps_nt_sb_bet',handler(self,self.ps_nt_sb_bet))
	self.network:register('rt_mpps_nt_sb_open_card',handler(self,self.ps_nt_sb_open_card))
	
	self.network:register('Login_child_game_res',handler(self,self.LoginChildGameRes))
	self.network:register('rt_mpps_status_res',handler(self,self.ps_status_res))
	self.network:register('rt_mpps_bet_res',handler(self,self.ps_bet_res))
	self.network:register('rt_mpps_nt_player_info_change',handler(self,self.ps_nt_player_info_change))
	self.network:register("nt_content_prompt",handler(self,self.nt_content_prompt))

end

function Logic:exit()
	skynet.sleep(100)
	self.network:close()
	skynet.exit()
end

function Logic:showError(data)
	if data.err_no and data.err_no > 0 then 
		print("_____error__",data)
		self:exit()
		return false
	end
	return true
end


--账号
function Logic:login(mid)
	local str = '{"ver":"1.0.0","appid":10100100,"action":"user.guestLogin","platform":1,"cmd":10000}'
	if mid then 
		str = '{"ver":"1.0.0","appid":10100100,"mid":'..mid..',"action":"user.guestLogin","platform":1,"cmd":10001}'
	end

	local data = {php_req={
 		id = 10002,
 		content = str,
	}}
	--print(data)
	self.network:send(data)
end

function Logic:loginChildGame(data)
	local data = {Login_child_game_req = {
				uid = self.playerid,
				--uid = 1201152,
				-- key = "d6a3c6e8ca45b46e4d6c9fcf4bd79408", 
				 --gameid = 9,
				 }}
	self.network:send(data)
end

function Logic:LoginChildGameRes(data)
	if not data.err_no or 0 == data.err_no then
		self:enterTable(self.tableid)
	end	
end 

function Logic:ps_status_res(data)
	if not self:showError(data) then 
		return 
	end
	print("_status_res____",data)
	self.player = data.seat_player
	self.config = data.config
	--self.chip = data.myself_info.chip
end


local num = 0
function Logic:php_res(data)
	-- print("___php_res",data)
	if not self:showError(data) then 
		return 
	end


	-- num = num + 1
	-- if num > 1 then 
	-- 	return
	-- end
	-- self:logout_req()
end

function Logic:nt_enter_gate(data)
	--进入大厅成功
	--local delay = math.random(100,300)
	--skynet.sleep(delay)
	--进入场次
	--self:enterTable(self.roomConf.id)
end

--登出
function Logic:logout_req()
	local data = {logout_req={playerid=1}}
	self.network:send(data)
end
function Logic:logout_res(data)
	if not self:showError(data) then 
		return 
	end
	self:login(self.playerid)
end

--场次信息
function Logic:room_state_req(gameid)
	local data = {room_state_req={
 		gameid=gameid, 		
	}}
	print(data)
	self.network:send(data)
end
function Logic:room_state_res(data)

end

function Logic:enterTable(tableid)
	local data = {enterprivate_room_req={
 		tablenum=tableid, gameid = 13		
	}}
	print("--------------------enterTable", data)
	self.network:send(data)		
end

function Logic:heartTick()	
	skynet.fork(function ()
		while true do
			skynet.sleep(self.heartTickTime)
			--print("------------------>heart", self.playerid, os.time())
			local data = {heart_req={
	 		tick=self.heartTickTime, 		
			}}
			self.network:send(data)	
		end		
	end)
end
function Logic:heart_res(data)	
	--print("---------------heart_res data", data)
end

function Logic:enter_table_res(data)
	print("_____enter_table_res___",data)
	-- if not self:showError(data) then 
	-- 	return 
	-- end
	-- print("____enter_table____",data)
	-- if data.err_no and data.err_no > 0 then 
	-- 	if data.err_no == 12 then 
	-- 		print("____________金币不足___",self.playerid,self.robotType)
	-- 		data = {playerid=self.playerid,robotType=self.robotType}
	-- 		self.nodeMessage:sendService('manager','change_money',data)
	-- 		self:exit()
	-- 		return 
	-- 	elseif data.err_no then 
	-- 		self:exit() 
	-- 		return
	-- 	end
	-- end
	if data.tableid > 0 then 
		self:heartTick()	
		self.tableid = data.tableid
		self:statusReq()
		skynet.sleep(math.random(100,200))
		data.playerid = self.playerid
		print("--------------------> Logic:enter_table_res",self.gameMgrName)
		skynet.send(self.gameMgrName, "lua", "player_enter_table_res", data)
	elseif data.err_no then 
		print("_____enter_table_res___ data.err_no:", data.err_no)
		self:nt_robotMrg_leaveTable()
		skynet.exit()
	end
end

--请求桌子状态
function Logic:statusReq()
	local data = {rt_mpps_status_req={
		tableid = self.tableid,
	}}
	print("statusReq___________", data)
	self.network:send(data)
end

function Logic:bankreq()
	local betRace = self.config.bank_rate[math.random(1, #self.config.bank_rate)]
	if not betRace then
		return
	end
	local data = {ps_race_bank_req={
		tableid = self.tableid,
		--rate = betRace,
		rate = betRace,
	}}
	--print("Logic:betreq___________", data)
	self.network:send(data)
end

function Logic:betreq()
	if self.bankerid == self.playerid then
		return
	end
	local betRace = self.config.bet_rate[math.random(0, #self.config.bet_rate)]
	if not betRace then
		return
	end
	local data = {ps_bet_req={
		tableid = self.tableid,
		rate = betRace,
	}}
	--print("Logic:betreq___________", data)
	self.network:send(data)
end

function Logic:ps_bet_res(data)
	--print("zfb_bet_res___________", data)
	if not data.err_no then

	end
end


----------------------------------------------------------------------------=


--离开桌子
function Logic:leaveTableReq()
	local data = {
	leave_table_req = {
			tableid = self.tableid,
		}
	}
	self.network:send(data)
end
function Logic:leave_table_res(data)
	-- print("__________ leave_table_res",data)
end


 
--换桌
function Logic:changetable_req()
	local data = {
		changetable_req={tableid = self.tableid}
	}
	self.network:send(data)
end
function Logic:changetable_res(data)

end


--个人属性改变
function Logic:nt_goods_change()
	
end


function Logic:nt_broadcast(data)
end

function Logic:nt_kick_table(data)
	--被踢出房间
	print("------------->nt_kick_table", data)
	self:nt_robotMrg_leaveTable()
	skynet.exit()
end

function Logic:closeByTime()
	--3 - 10分钟后退出游戏
	skynet.fork(function ()
		while (true) do 
			skynet.sleep(100)
			self.onlineTime = self.onlineTime + 1
			
		end
	end)
end

function Logic:ps_nt_rest(data)
	print("ps_nt_rest________")	
--[[	if self.chip < 10000 or self.onlineTime > self.leaveTableTime then
		if math.random(0, 100) > 50 then
			return
		end
		skynet.timeout(math.random(0, 300), function () 
			self:leaveTableReq()
			self:nt_robotMrg_leaveTable()			
		end)

	end]]--
end

function Logic:nt_robotMrg_leaveTable()
	skynet.sleep(100)
	if self.network then
		self.network:close()
	end
	skynet.send(self.gameMgrName, "lua", "player_leave_table_res", {playerid = self.playerid, tableid = self.tableid})
end

function Logic:ps_nt_end(data)
	print("_____ps_nt_end ps_nt_end data：", data)
	--[[self.chip = data.myself_settle.chip
	skynet.send(self.gameMgrName, "lua", "nt_settle", data)
	local code = 2
	if data.zhuang_settle.seatid == data.myself_settle.seatid then
		code = 1
	end
	self:zfb_change_chip_record({coin = data.myself_settle.winChips, code = code})
	]]--
end

function Logic:zfb_change_chip_record(data)
	local tableName = "robot_coin_log" .. os.date("%Y%m%d")
	--local sqlCreateTable = string.format("create table if not exists %s like robot_coin_log;", tableName)
	--local res =  skynet.call("mysql_log", "lua", "execute", sqlCreateTable)
	--print("--------------------------------res", res)
	--金币消耗原因code
	local sqlInsert = string.format("insert into %s (uid, coin, code, time,  game, gameid, gametype) values (%d, %d, %d, %d, %d, %d, %d);",
									 tableName, self.playerid, data.coin, data.code, os.time(), self.game, 3, self.tableid//10000)
	skynet.send("mysql_log", "lua", "execute", sqlInsert)
end

function Logic:ps_nt_start(data)
	print("---------ps_nt_start")
end

function Logic:ps_nt_bank(data)
	print("---------ps_nt_bank")
	local callback = function ()
		self:bankreq()	
	end
	local delay = math.random(50, 350)
	skynet.timeout(delay, callback)
end

function Logic:ps_nt_bet(data)
	print("---------ps_nt_bet")
	local callback = function ()
		if self.bankerid ~= self.playerid then
			self:betreq()
		end			
	end
	local delay = math.random(50, 350)
	skynet.timeout(delay, callback)
end

function Logic:ps_nt_left_card(data)
	print("ps_nt_left_card___________", data)
end

function Logic:ps_nt_seat_change(data)
	print("ps_nt_seat_change________", data)
	if data.seat_player.playerid == self.playerid then
		self.isSeatdown = data.seat
	end
	--skynet.call(self.gameMgrName, )
end

function Logic:ps_nt_bank_ok(data)
	print("____________ps_nt_bank_ok", data)
	self.bankerid = data.playerid
end

function Logic:ps_nt_random_bank_ok(data)
	print("ps_nt_random_bank_ok________________", data)
	self.bankerid = data.playerid
end

function Logic:ps_nt_sb_bank(data)
	print("ps_nt_sb_bank_____________", data)
end


function Logic:ps_nt_sb_bet(data)
	print("ps_nt_sb_bet_____________", data)
end

function Logic:ps_nt_sb_open_card(data)
	print("ps_nt_sb_open_card_____________", data)
end

function Logic:ps_nt_player_info_change(data)
	--print("zfb_nt_player_info_change________", data)
end

function Logic:nt_content_prompt(data)
	print("nt_content_prompt________", data)
end


function Logic:mgr_get_stauts()
	 self:statusReq()
end

function Logic:mgr_sitdown_req()
	--print("---------------------->mgr_sitdown_req")
	if not self.isSeatdown and self.chip > self.config.seat_min_money then
	 	self:sitdownReq()
	end
end

function Logic:mgr_standup_req()
	--print("---------------------->mgr_standup_req")
	if self.isSeatdown then
	 	self:standupReq()
	end
end

function Logic:zfb_player_res(data)
	--print("---------------------->zfb_player_res", data)
end




return Logic